/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.hadss.avoidarea;

import static org.hadss.avoidarea.constant.AvoidAreaType.TYPE_CUTOUT;
import static org.hadss.avoidarea.constant.AvoidAreaType.TYPE_NAVIGATION_INDICATOR;
import static org.hadss.avoidarea.constant.AvoidAreaType.TYPE_SYSTEM;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import org.hadss.avoidarea.utils.AvoidAreaUtils;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;

/**
 * RN方法实现类：提供RN方法安卓层实现
 *
 * @since 2025/5/12
 */
public class AvoidModule extends NativeAvoidModuleSpec {

    /**
     * RN Turbo模块名称
     */
    public static final String NAME = "RTNAvoidAreaRegion";

    private static final String TAG = AvoidModule.class.getSimpleName();

    private final Context context;

    AvoidModule(ReactApplicationContext applicationContext) {
        super(applicationContext);
        context = applicationContext.getApplicationContext();
    }

    @Override
    @NonNull
    public String getName() {
        return NAME;
    }

    @Override
    public WritableMap getWindowAvoidArea(double type) {
        Log.i(TAG, "getWindowAvoidArea type = " + AvoidAreaUtils.typeToStringName(type));
        WritableMap map = AvoidAreaUtils.getDefaultWritableMap();
        if (TYPE_SYSTEM == type) {
            AvoidAreaUtils.getAvoidAreaSystem(map, context);
        } else if (TYPE_CUTOUT == type) {
            AvoidAreaUtils.getAvoidAreaCutout(map, getCurrentActivity());
        } else if (TYPE_NAVIGATION_INDICATOR == type) {
            AvoidAreaUtils.getAvoidAreaNavigation(map, getCurrentActivity());
        }
        String writableMapJson = AvoidAreaUtils.getWritableMapJson(map);
        Log.d(TAG, "getWindowAvoidArea return : " + writableMapJson);

        // 手势区域和软键盘区域未找到相关的API获取
        return map;
    }

    /**
     * 监听窗口系统规避区域的变化
     */
    @Override
    public void addAvoidAreaListener() {
        Log.d(TAG, "addAvoidAreaListener call ");
    }

    /**
     * 移除监听窗口系统规避区域的变化
     */
    @Override
    public void removeAvoidAreaListener() {
        Log.d(TAG, "removeAvoidAreaListener call ");
    }
}